package sim

import (
	"context"
	"time"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/sim"
	"github.com/sagoo-cloud/sagooiot/internal/consts"
	"github.com/sagoo-cloud/sagooiot/internal/dao"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

type sSimSimCollect struct{}

func sSimSimCollectNew() *sSimSimCollect {
	return &sSimSimCollect{}
}
func init() {
	service.RegisterSimSimCollect(sSimSimCollectNew())
}

// GetSimCollectList 获取列表数据
func (s *sSimSimCollect) GetSimCollectList(ctx context.Context, in *model.GetSimCollectListInput) (total, page int, list []*model.SimCollectOutput, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.SimCollect.Ctx(ctx)
		if in.KeyWord != "" {
			m = m.WhereOrLike(dao.SimCollect.Columns().Iccid, "%"+in.KeyWord+"%")
			m = m.WhereOrLike(dao.SimCollect.Columns().Imsi, "%"+in.KeyWord+"%")
			m = m.WhereOrLike(dao.SimCollect.Columns().AccNumber, "%"+in.KeyWord+"%")
		}
		total, err = m.Count()
		if err != nil {
			err = gerror.New("获取总行数失败")
			return
		}
		page = in.PageNum
		if in.PageSize == 0 {
			in.PageSize = consts.PageSize
		}
		err = m.Page(page, in.PageSize).Order("last_change_date desc").Scan(&list)
		if err != nil {
			err = gerror.New("获取数据失败")
		}
	})
	return
}

// GetSimCollectById 获取指定ID数据
func (s *sSimSimCollect) SyncSimCollect(ctx context.Context) (res *sim.SyncSimCollectRes, err error) {
	return
}

// GetSimCollectById 获取指定ID数据
func (s *sSimSimCollect) SimFlowByDate(ctx context.Context, in *model.SimFlowByDate) (out *model.SimFlowByDateOut, err error) {
	res, err := service.SimSimHistoryTraffic().QueryHistoryTrafficByDate(ctx, in.Sdate, in.Edate, in.AccNumber, in.Types)
	if err != nil {
		return
	}
	out = new(model.SimFlowByDateOut)
	out.Data = res
	return
}

// GetSimCollectById 获取指定ID数据
func (s *sSimSimCollect) SimDetail(ctx context.Context, id int) (out *model.SimDetail, err error) {

	out = &model.SimDetail{}
	err = dao.SimCollect.Ctx(ctx).Where(dao.SimCollect.Columns().Id, id).Scan(&out.Sim)
	if err != nil {
		return
	}
	out.Sim.ActivationTime = out.Sim.ActivationTime[0:4] + "-" + out.Sim.ActivationTime[4:6] + "-" + out.Sim.ActivationTime[6:]

	// 当日流量
	dao.SimRealTraffic.Ctx(ctx).Where(dao.SimRealTraffic.Columns().AccessNumber, out.Sim.AccNumber).
		Where(dao.SimRealTraffic.Columns().MonthDay, 2).Fields("flow_amount real_flow").Scan(&out)

	// 昨日流量
	dao.SimHistoryTraffic.Ctx(ctx).Where(dao.SimHistoryTraffic.Columns().AccessNumber, out.Sim.AccNumber).
		Where(dao.SimHistoryTraffic.Columns().FlowDay, time.Now().AddDate(0, 0, -1).Format("20060102")).
		Fields("flow_amount yesterday_flow").Scan(&out)

	//  当月流量消耗统计
	dao.SimHistoryTraffic.Ctx(ctx).Where(dao.SimHistoryTraffic.Columns().AccessNumber, out.Sim.AccNumber).
		Where("left(flow_day,6) = ?", time.Now().Format("200601")).
		Fields("sum(flow_amount) month_flow").Scan(&out)

	// 本年流量消耗统计
	dao.SimHistoryTraffic.Ctx(ctx).Where(dao.SimRealTraffic.Columns().AccessNumber, out.Sim.AccNumber).
		Where("left(flow_day,4) = ?", time.Now().Format("2006")).
		Fields("sum(flow_amount) year_flow").Scan(&out)

	//当月流量消耗流水
	out.MonthDataList, err = service.SimSimHistoryTraffic().
		QueryHistoryTrafficByDate(ctx, time.Now().Format("2006-01")+"-01 00:00:00", time.Now().Format("2006-01-02 15:04:05"), out.Sim.AccNumber, gconv.Int(out.Sim.Types))
	if err != nil {
		return
	}
	// 当年流量消耗流水
	out.YearDataList, err = service.SimSimHistoryTraffic().
		QueryHistoryTrafficByDate(ctx, time.Now().Format("2006")+"-01-01 00:00:00", time.Now().Format("2006")+"-12-31 00:00:00", out.Sim.AccNumber, gconv.Int(out.Sim.Types))

	return
}

// AddSimCollect 添加数据
func (s *sSimSimCollect) AddSimCollect(ctx context.Context, in model.SimCollectAddInput) (err error) {
	_, err = dao.SimCollect.Ctx(ctx).Insert(in)
	return
}

// AddSimCollectByHttp 添加/更新 数据
func (s *sSimSimCollect) AddSimCollectByHttp(ctx context.Context, in *model.SimCollectAddInput) (err error) {
	total, err := dao.SimCollect.Ctx(ctx).Where(dao.SimCollect.Columns().Iccid, in.Iccid).Count()
	if err != nil {
		return
	}
	if total == 0 {
		_, err = dao.SimCollect.Ctx(ctx).Insert(in)
		return
	} else {
		_, err = dao.SimCollect.Ctx(ctx).Where(dao.SimCollect.Columns().Iccid, in.Iccid).FieldsEx(dao.SimCollect.Columns().Iccid).Update(in)
		return
	}

}

// EditSimCollect 修改数据
func (s *sSimSimCollect) EditSimCollect(ctx context.Context, in model.SimCollectEditInput) (err error) {
	dao.SimCollect.Ctx(ctx).FieldsEx(dao.SimCollect.Columns().Id).Where(dao.SimCollect.Columns().Id, in.Id).Update(in)
	return
}

// DeleteSimCollect 删除数据
func (s *sSimSimCollect) DeleteSimCollect(ctx context.Context, Ids []int) (err error) {
	_, err = dao.SimCollect.Ctx(ctx).Delete(dao.SimCollect.Columns().Id+" in (?)", Ids)
	return
}
